package com.androturka;

import java.util.List;

import android.app.Dialog;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class Harita extends MapActivity {
	/** Called when the activity is first created. */

	private static final String LATITUDE 							= "latitude";
	private static final String LONGITUDE 						= "longitude";
	private static final String ACTION_SHOW_LOCATION_ONMAP 		= "com.listview.showLocationMap";
	private static final String LOCATION 							= "com.listview.location";
	private static final String DATE 								= "com.listview.date";
	private static final String MAGNITUDE 						= "com.listview.magnitude";
	private static final String ACTION_SHOW_ALL_ONMAP 			= "com.listview.showAllMap";
	private static final String ACTION_SHOW_NOTIFICATION_ONMAP 	= "com.listview.showNotificationMap";

	private MapView mapView;
	private TextView locationText;
	private TextView dateText;

	Drawable earthquakemarker;
	Drawable marker_green;
	Drawable marker_yellow;
	Drawable marker_orange;
	Drawable marker_red;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		earthquakemarker = this.getResources().getDrawable(R.drawable.earthquakemarker);
		marker_green = this.getResources().getDrawable(R.drawable.marker_green);
		marker_yellow = this.getResources().getDrawable(R.drawable.marker_yellow);
		marker_orange = this.getResources().getDrawable(R.drawable.marker_orange);
		marker_red = this.getResources().getDrawable(R.drawable.marker_red);

		String action = getIntent().getAction();

		super.onCreate(savedInstanceState);
		//setContentView(R.layout.harita);
		setContentView(R.layout.harita);
		mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);
		locationText = (TextView)findViewById(R.id.mapLocation);
		dateText = (TextView)findViewById(R.id.mapDate);

		if (action.equals(ACTION_SHOW_LOCATION_ONMAP) == true) 
		{
			Intent i = getIntent();

			final EarthQuakeItemizedOverlay itemizedOverlay = new EarthQuakeItemizedOverlay(
					earthquakemarker, mapView.getContext());
			double latitude = i.getDoubleExtra(LATITUDE, 0);
			double longitude = i.getDoubleExtra(LONGITUDE, 0);
			final String location = i.getStringExtra(LOCATION);
			final String date = i.getStringExtra(DATE);
			final double magnitude = i.getDoubleExtra(MAGNITUDE, 0);
			
			final GeoPoint eqCoord = new GeoPoint(
					(int) (latitude * 1e6), (int) (longitude * 1e6));

			locationText.setText(magnitude + " - " + location);
			dateText.setText(date);

			EarthQuakeOverlayItem overlayitem = new EarthQuakeOverlayItem(
					eqCoord, location, date, magnitude);
			itemizedOverlay.addOverlay(overlayitem);
			List<Overlay> mapOverLays = mapView.getOverlays();
			mapOverLays.clear();
			mapOverLays.add(itemizedOverlay);
			mapView.getController().setCenter(eqCoord);
			mapView.getController().setZoom(10);
			
			/**************************************/
			ImageButton shareButton = (ImageButton)findViewById(R.id.shareButton);
			shareButton.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					String text = magnitude + " - " + location + " - " + date + "   Geçmiş Olsun. " + " Android Deprem uygulaması ile paylaşıldı. https://play.google.com/store/apps/details?id=com.androturka" ;
					String subject = "Deprem Var";
					Intent sharingIntent = new Intent(Intent.ACTION_SEND);
					sharingIntent.setType("text/plain");
					sharingIntent.putExtra(Intent.EXTRA_TEXT, text);
					sharingIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
					startActivity(Intent.createChooser(sharingIntent, "Paylaş"));	
				}
			});
			
			/**************************************/

		}
		else if (action.equals(ACTION_SHOW_NOTIFICATION_ONMAP) == true) 
		{
			NotificationManager manager;
			manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
			
			Intent i = getIntent();


			final EarthQuakeItemizedOverlay itemizedOverlay = new EarthQuakeItemizedOverlay(
					earthquakemarker, mapView.getContext());
			double latitude = i.getDoubleExtra(LATITUDE, 0);
			double longitude = i.getDoubleExtra(LONGITUDE, 0);
			String location = i.getStringExtra(LOCATION);
			String date = i.getStringExtra(DATE);
			double magnitude = i.getDoubleExtra(MAGNITUDE, 0);
			int notificationID = i.getIntExtra("notificationID", 0);

			manager.cancel(notificationID);

			final GeoPoint eqCoord = new GeoPoint(
					(int) (latitude * 1e6), (int) (longitude * 1e6));

			locationText.setText(magnitude + " - " + location);
			dateText.setText(date);

			EarthQuakeOverlayItem overlayitem = new EarthQuakeOverlayItem(
					eqCoord, location, date, magnitude);
			itemizedOverlay.addOverlay(overlayitem);
			List<Overlay> mapOverLays = mapView.getOverlays();
			mapOverLays.clear();
			mapOverLays.add(itemizedOverlay);
			mapView.getController().setCenter(eqCoord);
			mapView.getController().setZoom(10);

		}
		else if (action.equals(ACTION_SHOW_ALL_ONMAP)==true)
		{
			locationText.setVisibility(View.GONE);
			dateText.setVisibility(View.GONE);
			/*
			// BU KISIM FARKLI BUYUKLUKLERI FARKLI RENKLERDE GOSTERMEK ICIN
			Intent i = getIntent();

			final EarthQuakeItemizedOverlay itemizedOverlay_green = new EarthQuakeItemizedOverlay(
					marker_green, mapView.getContext());
			final EarthQuakeItemizedOverlay itemizedOverlay_yellow = new EarthQuakeItemizedOverlay(
					marker_yellow, mapView.getContext());
			final EarthQuakeItemizedOverlay itemizedOverlay_orange = new EarthQuakeItemizedOverlay(
					marker_orange, mapView.getContext());
			final EarthQuakeItemizedOverlay itemizedOverlay_red = new EarthQuakeItemizedOverlay(
					marker_red, mapView.getContext());
			
			GeoPoint turkey_center = new GeoPoint(
					(int) (38 * 1e6), (int) (35.5 * 1e6));
			
			String[] latitude = i.getStringArrayExtra(LATITUDE);
			String[] longitude = i.getStringArrayExtra(LONGITUDE);
			String[] location = i.getStringArrayExtra(LOCATION);
			String[] date = i.getStringArrayExtra(DATE);
			String[] magnitude = i.getStringArrayExtra(MAGNITUDE);
			
			int eartquakeCount=location.length;
			
			for (int ii = 0; ii < eartquakeCount; ii++) {
				double lat=Double.parseDouble(latitude[ii]);
				double lon=Double.parseDouble(longitude[ii]);
				double mag=Double.parseDouble(magnitude[ii]);
				GeoPoint eqCoord = new GeoPoint(
						(int) (lat * 1e6), (int) (lon * 1e6));
				EarthQuakeOverlayItem overlayitem = new EarthQuakeOverlayItem(
						eqCoord, location[ii], date[ii], mag);
				if ( mag < 3.0 )
					itemizedOverlay_green.addOverlay(overlayitem);
				else if ( mag < 5.0 )
					itemizedOverlay_yellow.addOverlay(overlayitem);
				else if ( mag < 6.0 )
					itemizedOverlay_orange.addOverlay(overlayitem);
				else
					itemizedOverlay_red.addOverlay(overlayitem);
			}
			List<Overlay> mapOverLays = mapView.getOverlays();
			mapOverLays.clear();
			mapOverLays.add(itemizedOverlay_green);
			mapOverLays.add(itemizedOverlay_yellow);
			mapOverLays.add(itemizedOverlay_orange);
			mapOverLays.add(itemizedOverlay_red);
			mapView.getController().setCenter(turkey_center);
			mapView.getController().setZoom(6);
			//mapView.getController().setZoom(7);
			*/
			
			// BU KISIM AYNI MARKER ILE GOSTERIYOR.
			Intent i = getIntent();

			final EarthQuakeItemizedOverlay itemizedOverlay= new EarthQuakeItemizedOverlay(
					marker_red, mapView.getContext());

			GeoPoint turkey_center = new GeoPoint(
					(int) (38 * 1e6), (int) (35.5 * 1e6));
			
			String[] latitude = i.getStringArrayExtra(LATITUDE);
			String[] longitude = i.getStringArrayExtra(LONGITUDE);
			String[] location = i.getStringArrayExtra(LOCATION);
			String[] date = i.getStringArrayExtra(DATE);
			String[] magnitude = i.getStringArrayExtra(MAGNITUDE);
			
			int eartquakeCount=location.length;
			
			for (int ii = 0; ii < eartquakeCount; ii++) {
				double lat=Double.parseDouble(latitude[ii]);
				double lon=Double.parseDouble(longitude[ii]);
				double mag=Double.parseDouble(magnitude[ii]);
				GeoPoint eqCoord = new GeoPoint(
						(int) (lat * 1e6), (int) (lon * 1e6));
				EarthQuakeOverlayItem overlayitem = new EarthQuakeOverlayItem(
						eqCoord, location[ii], date[ii], mag);
					itemizedOverlay.addOverlay(overlayitem);
			}
			List<Overlay> mapOverLays = mapView.getOverlays();
			mapOverLays.clear();
			mapOverLays.add(itemizedOverlay);
			mapView.getController().setCenter(turkey_center);
			mapView.getController().setZoom(5);
			//mapView.getController().setZoom(7);
		}
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();

		inflater.inflate(R.menu.haritamenu, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		final Dialog dialog = new Dialog(this);
		
		switch (item.getItemId()) {
			case R.id.hakkinda:				
				dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
				dialog.setContentView(R.layout.about);
				dialog.show();
	
				Button positiveButton = (Button) dialog
						.findViewById(R.id.positiveButton);
	
				positiveButton.setOnClickListener(new OnClickListener() {
	
					@Override
					public void onClick(View arg0) {
						dialog.cancel();
					}
				});
				break;
			case R.id.refresh:
				Intent refreshIntent = new Intent(Harita.this, Main.class);
				refreshIntent.setAction("android.intent.action.MAIN");
				startActivity(refreshIntent); 

				break;
			/*	
			case R.id.ayarlar:
				Intent settingsIntent = new Intent(getApplicationContext(), Settings.class);
				startActivity(settingsIntent);
				break;
			*/
		} //switch


		return super.onOptionsItemSelected(item);
	}	

}